<?php
declare(strict_types=1);

namespace app\index\controller;

use aphp\core\Jump;

class Index
{
    use Jump;

    public function index(string $route = 'index/index')
    {
        //dd($method, $req);
        // 站点配置
        $site = site();
        // 默认SEO
        $topic = [
            'seo_title' => $site['site_title'],
            'seo_kw' => $site['site_kw'],
            'seo_desc' => $site['site_desc'],
            'tpl' => $route,
        ];
        // 首页
        if ($route == 'index/index') {
            return view($route)->with('topic', $topic);
        }
        // 获取栏目
        [$sign, $action] = explode('/', $route);
        $topic = widget('common.topic')->get($sign);
        if (!empty($topic)) {
            $tpl = ($action == 'index') ? $topic['tpl'] : $topic['tpl'] . '_' . $action;
            if (is_file(VIEW_PATH . '/' . __THEME__ . '/index/' . $tpl . '.html')) {
                $topic['tpl'] = 'index/' . $tpl;
            } else  {
                $topic['tpl'] = ($topic['type'] == 1) ? 'index/about' : 'index/404';
            }
        }
        if (empty($topic) || $topic['tpl'] == 'index/404') {
            if ($this->isAjax()) {
                $this->_json(404, '页面不存在');
            }
            return view('index/404')->with('topic', $topic);
        }
        $table = $topic['model_table'];
        if (!empty($table) && $topic['type'] == 2) {
            // 列表详情
            if ($action == 'detail') {
                $id = input('get.id', 0, 'intval');
                if ($id <= 0) {
                    $this->error('ID参数必须');
                }
                $vo = db($table)->where('id', $id)->where('status=1')->find();
                if (!$vo) {
                    $this->error('记录不存在');
                }
                $topic['seo_title'] = $vo['title'] . ' - ' . $site['site_name'];
                if (!empty($vo['seo_kw'])) {
                    $topic['seo_kw'] = $vo['seo_kw'];
                }
                if (!empty($vo['seo_desc'])) {
                    $topic['seo_desc'] = $vo['seo_desc'];
                }
                if (isset($vo['read_count']) && $id != cookie($table . '-read-id')) {
                    db($table)->where('id', $id)->setInc('read_count');
                    cookie($table . '-read-id', $id);
                    $vo['read_count']++;
                }
                return view($topic['tpl'])->with(['topic' => $topic, 'vo' => $vo]);
            }
            // 列表
            if ($action == 'index') {
                // 列表条件
                $page_where = [];
                $page_type = 1;
                if ($topic['id'] != $topic['nav_pid'] && $topic['is_parent'] == 0) {
                    $page_where['tid'] = $topic['id'];
                    $page_type = 2;
                }
                $keyword = input('get.keyword', '', 'clear_html');
                if (!empty($keyword)) {
                    $page_where[] = ['title', 'like', "%{$keyword}%"];
                    $page_type = 3;
                }
                $tag = input('get.tag', '', 'clear_html');
                if (!empty($tag)) {
                    $tags = widget('common.tags')->get('', ['field' => 'id,title']);
                    $tag_id = $tags[$tag]['id'] ?? 0;
                    if ($tag_id > 0) {
                        $page_where[] = ['tags_ids', 'find_in_set', $tag_id];
                    }
                    $tag = $tags[$tag]['title'] ?? $tag;
                    $page_type = 4;
                }
                return view($topic['tpl'])->with(['topic' => $topic, 'page_where' => $page_where, 'page_type' => $page_type, 'keyword' => $keyword, 'tag' => $tag]);
            }
        }
        return view($topic['tpl'])->with('topic', $topic);
    }
}